
#include "precompiled.h"
#include "nativetask.h"


#include "nativeplugin.h"
#include "engine.h"
#include "windowsmanager.h"
#include "tasks.h"
#include "script/contextimpl.h"


class NativeTaskPrivate{
	public:
	NativeTaskPrivate(NativePlugin* ev):
		ext(ev)
	{}
		~NativeTaskPrivate() {}
		NativePlugin* ext;

		QUrl url;

		void run()
		{
			ext->start();
		}
};


NativeTask::NativeTask(NativePlugin* ext, const QUrl& url) :
	Opl::Core::Task(0), O_PRIVATE_NAME(new NativeTaskPrivate(ext))
{	
	O_D(NativeTask);
	d->url = url;
	//d->environment = environment;
	setup();
}

NativeTask::~NativeTask()
{
	O_PRIVATE_DELETE;
}

void NativeTask::run()
{    
    O_D(NativeTask);
    d->run();
    while(!quitNow())
    {
        msleep(10);
    }
}

bool NativeTask::isValid()const
{
    O_C(NativeTask);
    return d->ext->valid();
}

void NativeTask::setup()
{
    O_D(NativeTask);
    //d->environment->initialize();
    connect(this, SIGNAL(done()), this, SLOT(cleanup()));
    d->ext->initialize();
}


void NativeTask::cleanup()
{
    O_D(NativeTask);
    d->ext->stop();
}
